<?php

namespace addons\TinyShare\merchant\controllers;

use Yii;
use yii\data\Pagination;
use common\enums\StatusEnum;
use common\traits\MerchantCurd;
use common\models\base\SearchModel;
use addons\TinyShare\common\models\ShareCate;
use addons\TinyShare\common\models\ShareTag;
use addons\TinyShare\common\models\ShareTagMap;
use addons\TinyShare\common\models\ShareContent;
use addons\TinyShare\common\models\ShareContentImage;
use addons\TinyShare\common\models\ShareContentVedio;
use addons\TinyShare\common\models\ShareCollect;
use addons\TinyShare\common\models\ShareLike;
use addons\TinyShare\common\enums\ContentTypeEnum;

class ShareContentController extends BaseController
{
	use MerchantCurd;

    /**
     * @var ShareContent
     */
    public $modelClass = ShareContent::class;
	
	/**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => ShareContent::class,
            'scenario' => 'default',
            'partialMatchAttributes' => ['title'], // 模糊查询
            'defaultOrder' => [
                'sort' => SORT_ASC,
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED])
            ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'cates' => ShareCate::getDropDown()
        ]);
    }
	
	/**
     * 编辑/创建
     *
     * @return string|\yii\console\Response|\yii\web\Response
     * @throws \yii\db\Exception
     */
    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id', null);
        $model = $this->findModel($id);

        // 设置选中标签
        $tagMap = ShareTagMap::getTagsByContentId($id);
        $model->tags = array_column($tagMap, 'tag_id');
        // 推荐位
        $positionExplain = ShareContent::$positionExplain;
        $keys = [];
        foreach ($positionExplain as $key => $value) {
            if (ShareContent::checkPosition($key, $model->position)) {
                $keys[] = $key;
            }
        }
        $model->position = $keys;
		$model->content_type = ContentTypeEnum::ARTICLE;

        if ($model->load($request->post()) && $model->save()) {
            // 更新文章标签
            ShareTagMap::addTags($model->id, $model->tags);
            return $this->redirect(['index']);
        }

        return $this->render($this->action->id, [
            'model' => $model,
            'cates' => ShareCate::getDropDown(),
            'positionExplain' => $positionExplain,
            'tags' => ShareTag::getCheckTags(),
        ]);
    }

	/**
     * 编辑/创建
     *
     * @return string|\yii\console\Response|\yii\web\Response
     * @throws \yii\db\Exception
     */
    public function actionEditImage()
    {
        $request = Yii::$app->request;
        $id = $request->get('id', null);
		
		if (empty($id) || empty(($model = ShareContentImage::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
            $model = new ShareContentImage();
            $model->loadDefaultValues();
        }

        // 设置选中标签
        $tagMap = ShareTagMap::getTagsByContentId($id);
        $model->tags = array_column($tagMap, 'tag_id');
        // 推荐位
        $positionExplain = ShareContent::$positionExplain;
        $keys = [];
        foreach ($positionExplain as $key => $value) {
            if (ShareContent::checkPosition($key, $model->position)) {
                $keys[] = $key;
            }
        }
        $model->position = $keys;
		$model->content_type = ContentTypeEnum::IMAGE;
		$model->cover=unserialize($model->cover);

        if ($model->load($request->post()) && $model->save()) {
            // 更新文章标签
            ShareTagMap::addTags($model->id, $model->tags);
            return $this->redirect(['index']);
        }

        return $this->render($this->action->id, [
            'model' => $model,
            'cates' => ShareCate::getDropDown(),
            'positionExplain' => $positionExplain,
            'tags' => ShareTag::getCheckTags(),
        ]);
    }

	/**
     * 编辑/创建
     *
     * @return string|\yii\console\Response|\yii\web\Response
     * @throws \yii\db\Exception
     */
    public function actionEditVedio()
    {
        $request = Yii::$app->request;
        $id = $request->get('id', null);
        
		if (empty($id) || empty(($model = ShareContentVedio::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
            $model = new ShareContentVedio();
            $model->loadDefaultValues();
        }

        // 设置选中标签
        $tagMap = ShareTagMap::getTagsByContentId($id);
        $model->tags = array_column($tagMap, 'tag_id');
        // 推荐位
        $positionExplain = ShareContent::$positionExplain;
        $keys = [];
        foreach ($positionExplain as $key => $value) {
            if (ShareContent::checkPosition($key, $model->position)) {
                $keys[] = $key;
            }
        }
        $model->position = $keys;
		$model->content_type = ContentTypeEnum::VEDIO;

        if ($model->load($request->post()) && $model->save()) {
            // 更新文章标签
            ShareTagMap::addTags($model->id, $model->tags);
            return $this->redirect(['index']);
        }

        return $this->render($this->action->id, [
            'model' => $model,
            'cates' => ShareCate::getDropDown(),
            'positionExplain' => $positionExplain,
            'tags' => ShareTag::getCheckTags(),
        ]);
    }
	

	/**
     * 删除
     *
     * @param $id
     * @return mixed
     */
    public function actionHide($id)
    {
        $model = $this->findModel($id);
        $model->status = StatusEnum::DELETE;
        if ($model->save()) {
            return $this->message("删除成功", $this->redirect(['index']));
        }

        return $this->message("删除失败", $this->redirect(['index']), 'error');
    }
}
